package enhance.plus.util;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import common.core.util.CommonUtil;
import common.core.util.CustomizeRuntimeException;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liubo
 * @created 2020-01-18 15:42
 */
@Slf4j
public class ConditionHelper {

   public static final String key_id = "id";
   public static final String key_idList="idList";

   public static final String key_entitys="entitys";
   public static final String key_page="page";
   public static final String key_condition="condition";
   public static final String key_wrapper ="wrapper";

   public static final String key_operator_eq="eq";

   public static synchronized Serializable getId(Condition condition){
      return (Serializable) condition.get(key_id);
   }
   public static synchronized List<Serializable> getIdList(Condition condition){
      return (List<Serializable>) condition.get(key_idList);
   }

   public static synchronized List getEntitys(Condition condition){
      return (List)condition.get(key_entitys);
   }

   public static synchronized IPage getPage(Condition condition){
      return (IPage)condition.get(key_page);
   }

   public static synchronized Wrapper getWrapper(Condition condition,QueryWrapper wapper){
      ArrayList<LinkedHashMap<String,Object>> list = (ArrayList<LinkedHashMap<String,Object>>) condition.get(ConditionHelper.key_wrapper);
      if(CommonUtil.isNotEmpty(list)){
         for (Map<String, Object> map : list) {
            ConditionInfo info = CommonUtil.convertMapToBean(map,ConditionInfo.class);
            if(key_operator_eq.equals(info.getOperator())){
               wapper.eq(info.getCloumn(),info.getValue());
            }
         }
      }
      return wapper;
   }

   public static synchronized Wrapper getPageWrapper(Condition condition,QueryWrapper wapper){
      return ConditionHelper.getWrapper((Condition) condition.get(key_condition),wapper);
   }

   public static synchronized Map<String , Object> getColumnMap(Condition condition){
      List<Map<String,Object>> list = (List<Map<String, Object>>) condition.get(ConditionHelper.key_wrapper);
      Map<String,Object> columnMap = new LinkedHashMap<>();
      if(CommonUtil.isNotEmpty(list)){
         for (Map<String, Object> map : list) {
            ConditionInfo info = CommonUtil.convertMapToBean(map,ConditionInfo.class);
            if(key_operator_eq.equals(info.getOperator())){
               columnMap.put(info.getCloumn(),info.getValue());
            }else{
               log.info("getColumnMap 中操作符只能存在 eq 当前参数为:{}", CommonUtil.convertObjectToJson(condition));
               throw CustomizeRuntimeException.exception("查询错误,请联系管理员");
            }
         }
      }
      return columnMap;
   }
}
